package com.yc.navy.blog.blog.controller;

import com.yc.navy.blog.blog.bean.UserInfo;
import com.yc.navy.blog.blog.enums.ResultEnum;
import com.yc.navy.blog.blog.service.IUserInfoService;
import com.yc.navy.blog.blog.util.ConstantInfo;
import com.yc.navy.blog.blog.util.SendEmailUtil;
import com.yc.navy.blog.blog.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private SendEmailUtil sendEmailUtil;

    @PostMapping("/send")
    public ResultVo send(String email, String name, HttpSession session){
        StringBuilder code=new StringBuilder();
        Random rd=new Random();
        for (int i=0;i<6;++i){
            code.append(rd.nextInt(10));
        }
        if(sendEmailUtil.sendEmail(email,code.toString(),name)){
            session.setAttribute("code",code.toString());
            //启用定时器，3分钟后清空session
           TimerTask task=new TimerTask() {
               @Override
               public void run() {
                   session.removeAttribute(ConstantInfo.EMAIL_CODE);
               }
           };//()=>{session.removeAttribute("code");}
            Timer timer=new Timer();
            timer.schedule(task,180000);
            return new ResultVo(ResultEnum.SUCCESS);
        }
        return new ResultVo(ResultEnum.ERROR);
    }

    @PostMapping("/login")
    public ResultVo login(UserInfo uf,HttpSession session,String code){
        Object obj=session.getAttribute(ConstantInfo.CHECKCODE);
        if (!code.equals(String.valueOf(obj))){
            return new ResultVo(ResultEnum.CODE_ERROR);
        }
        UserInfo user=userInfoService.login(uf);
        if (user==null){
            return  new ResultVo(ResultEnum.LOGIN_ERROR);
        }
        session.setAttribute("currentLoginUser",user);
        return new ResultVo(ResultEnum.SUCCESS);
    }

    @PostMapping("/reg")
    public ResultVo reg(UserInfo uf,String code,HttpSession session){
        Object obj=session.getAttribute(ConstantInfo.EMAIL_CODE);
        if (obj==null){
            return new ResultVo(ResultEnum.CODE_TIMEOUT);
        }
        if (!code.equals(String.valueOf(obj))){
            return new ResultVo(ResultEnum.CODE_ERROR);
        }
        int result= userInfoService.reg(uf);
        if (result==100){
            return new ResultVo(ResultEnum.CHECK_ERROR);
        }
        if (result>0){
            return new ResultVo(ResultEnum.SUCCESS);
        }
        return new ResultVo(ResultEnum.ERROR);
    }
    @GetMapping("/check")
    public ResultVo check(HttpSession session){
        Object obj=session.getAttribute("currentLoginUser");
        if (obj==null){
            return new ResultVo(ResultEnum.LOGIN_TIMEOUT);
        }
        return new ResultVo(ResultEnum.SUCCESS,obj);
    }

}
